package com.mathmodeling.config;

import com.mathmodeling.entity.User;
import com.mathmodeling.entity.Topic;
import com.mathmodeling.entity.Contest;
import com.mathmodeling.repository.UserRepository;
import com.mathmodeling.repository.TopicRepository;
import com.mathmodeling.repository.ContestRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.UUID;

@Component
public class DataInitializer implements CommandLineRunner {
    
    @Autowired
    private UserRepository userRepository;
    
    @Autowired
    private TopicRepository topicRepository;
    
    @Autowired
    private ContestRepository contestRepository;
    
    @Autowired
    private PasswordEncoder passwordEncoder;
    
    @Override
    @Transactional
    public void run(String... args) throws Exception {
        System.out.println("🚀 开始初始化测试数据...");
        
        // 检查是否已存在测试用户
        if (!userRepository.existsByUsername("admin")) {
            // 创建管理员用户
            User admin = new User();
            admin.setId(UUID.randomUUID().toString());
            admin.setUsername("admin");
            admin.setPassword(passwordEncoder.encode("123456"));
            admin.setRole("admin");
            admin.setCreatedAt(LocalDateTime.now());
            userRepository.save(admin);
            System.out.println("✅ 管理员用户创建成功: admin/123456");
        } else {
            System.out.println("ℹ️ 管理员用户已存在");
        }
        
        if (!userRepository.existsByUsername("teacher1")) {
            // 创建教师用户
            User teacher1 = new User();
            teacher1.setId(UUID.randomUUID().toString());
            teacher1.setUsername("teacher1");
            teacher1.setPassword(passwordEncoder.encode("123456"));
            teacher1.setRole("teacher");
            teacher1.setCreatedAt(LocalDateTime.now());
            userRepository.save(teacher1);
            System.out.println("✅ 教师用户创建成功: teacher1/123456");
        } else {
            System.out.println("ℹ️ 教师用户已存在");
        }
        
        if (!userRepository.existsByUsername("student1")) {
            // 创建学生用户
            User student1 = new User();
            student1.setId(UUID.randomUUID().toString());
            student1.setUsername("student1");
            student1.setPassword(passwordEncoder.encode("123456"));
            student1.setRole("student");
            student1.setCreatedAt(LocalDateTime.now());
            userRepository.save(student1);
            System.out.println("✅ 学生用户创建成功: student1/123456");
        } else {
            System.out.println("ℹ️ 学生用户已存在");
        }
        
        // 初始化竞赛测试数据
        String contestId = "contest001";
        if (!contestRepository.existsById(contestId)) {
            Contest contest = new Contest();
            contest.setId(contestId);
            contest.setTitle("2025年校赛");
            contest.setStartTime(java.time.LocalDate.of(2025, 1, 15));
            contest.setEndTime(java.time.LocalDate.of(2025, 1, 20));
            contest.setResultRelease(java.time.LocalDate.of(2025, 1, 25));
            contest.setCreatedAt(LocalDateTime.now());
            contestRepository.save(contest);
            System.out.println("✅ 竞赛数据创建成功: 2025年校赛");
        } else {
            System.out.println("ℹ️ 竞赛数据已存在");
        }
        
        // 初始化题目测试数据
        if (!topicRepository.existsByContestIdAndName(contestId, "A")) {
            // 创建题目A
            Topic topicA = new Topic();
            topicA.setId(UUID.randomUUID().toString());
            topicA.setContestId(contestId);
            topicA.setName("A");
            topicA.setDesc("人工智能主题");
            topicA.setFilePath("/topics/contest001/A.pdf");
            topicA.setCreatedAt(LocalDateTime.now());
            topicRepository.save(topicA);
            System.out.println("✅ 题目A创建成功");
        }
        
        if (!topicRepository.existsByContestIdAndName(contestId, "B")) {
            // 创建题目B
            Topic topicB = new Topic();
            topicB.setId(UUID.randomUUID().toString());
            topicB.setContestId(contestId);
            topicB.setName("B");
            topicB.setDesc("大数据主题");
            topicB.setFilePath("/topics/contest001/B.pdf");
            topicB.setCreatedAt(LocalDateTime.now());
            topicRepository.save(topicB);
            System.out.println("✅ 题目B创建成功");
        }
        
        if (!topicRepository.existsByContestIdAndName(contestId, "C")) {
            // 创建题目C
            Topic topicC = new Topic();
            topicC.setId(UUID.randomUUID().toString());
            topicC.setContestId(contestId);
            topicC.setName("C");
            topicC.setDesc("物联网主题");
            topicC.setFilePath("/topics/contest001/C.pdf");
            topicC.setCreatedAt(LocalDateTime.now());
            topicRepository.save(topicC);
            System.out.println("✅ 题目C创建成功");
        }
        
        if (!topicRepository.existsByContestIdAndName(contestId, "D")) {
            // 创建题目D
            Topic topicD = new Topic();
            topicD.setId(UUID.randomUUID().toString());
            topicD.setContestId(contestId);
            topicD.setName("D");
            topicD.setDesc("区块链主题");
            topicD.setFilePath("/topics/contest001/D.pdf");
            topicD.setCreatedAt(LocalDateTime.now());
            topicRepository.save(topicD);
            System.out.println("✅ 题目D创建成功");
        }
        
        if (!topicRepository.existsByContestIdAndName(contestId, "E")) {
            // 创建题目E
            Topic topicE = new Topic();
            topicE.setId(UUID.randomUUID().toString());
            topicE.setContestId(contestId);
            topicE.setName("E");
            topicE.setDesc("其他主题");
            topicE.setFilePath("/topics/contest001/E.pdf");
            topicE.setCreatedAt(LocalDateTime.now());
            topicRepository.save(topicE);
            System.out.println("✅ 题目E创建成功");
        }
        
        System.out.println("🎉 数据初始化完成！");
    }
} 